<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.rule_id ? $t('编辑') : $t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="500px" top="5vh" @close='close'>
    <div class="dialogContentBoxMaxHeight90">
      <el-form ref="form" :inline="true" :model="record" label-width="130px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('协议名称')" class="pniocmForm" prop="proto">
          <span style="color:#999;">{{ rule[record.proto] }}</span>
        </el-form-item>
        <el-form-item :label="$t('动作')" class="pniocmForm" prop="action">
          <el-select class="cdtLength" v-model="record.action" ref="action" size='small' filterable>
            <el-option v-for="(value, key) in actionList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('功能码')" class="pniocmForm" prop="func">
          <el-select class="pniocmLength" v-model="record.func" ref="func" size='small' filterable>
            <el-option v-for="(value, key) in pniocmFuncList" :key="key" :label="value" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('模块属性')" class="pniocmForm" prop="block_type">
          <el-select class="pniocmLength" v-model="record.block_type" ref="blockType" size='small' filterable>
            <el-option v-for="item in pniocmBlockTypeList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('操作对象')" class="pniocmForm" prop="index">
          <el-select class="pniocmLength" v-model="record.index" ref='recordIndex' size='small' filterable>
            <el-option v-for="item in pniocmIndexList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>

  </el-dialog>
</template>
<script>
import ruleArray from '../../ruleArray'
import { selectClass } from '@/utils/input-class.js'
export default {
  name: 'PniocmDialog',
  data () {
    return {
      dialogVisible: false,
      record: {
        func: '',
        action: 'accept',
        proto: 'pnio_cm',
        rule_id: null,
        block_type: '',
        index: ''
      },
      recordCopy: {
        func: '',
        action: 'accept',
        proto: 'pnio_cm',
        rule_id: null,
        block_type: '',
        index: ''
      },
      rule: ruleArray.rule,
      pniocmBlockTypeList: ruleArray.pniocmBlockTypeList,
      pniocmIndexList: ruleArray.pniocmIndexList,
      pniocmFuncList: ruleArray.pniocmFuncList,
      actionList: { 'accept': '允许', 'alert': '告警' },
      rules: {
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const selectData = ['action', 'func', 'blockType', 'recordIndex']
        selectClass(this.$refs, selectData)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          var formData = JSON.parse(JSON.stringify(this.record))
          this.$emit('addConfigRuleMap', formData)
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.$emit('childrenClose')
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style>
.pniocmLength {
  width: 250px;
}
</style>
